package com.zhongshengnetwork.rightbe.common;

import android.content.Context;
import android.os.Build;
import android.provider.Settings;

import com.zhongshengnetwork.rightbe.Adv.model.AdvertUtils;
import com.zhongshengnetwork.rightbe.Adv.model.SPManageUtils;
import com.zhongshengnetwork.rightbe.CustomApplication;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.UUID;

/**
 * Created by luojingxing on 2018/4/23.
 */

public class DeviceUtils {

    /**
     * 生成令牌方法
     *
     * @return 返回令牌字符串
     */
    public static String getDeviceID() {
        String deviceID = SPManageUtils.getInstance(CustomApplication.mContext).getSPString(AdvertUtils.DeviceID,"");
        if(CommonUtils.isEmpty(deviceID)){
            deviceID = getUniqueId(CustomApplication.mContext);
            if (deviceID == null || deviceID.length() == 0){
                String token = UUID.randomUUID().toString();
                deviceID = token.replace("-", "").toUpperCase();
            }
            SPManageUtils.getInstance(CustomApplication.mContext).putSPString(AdvertUtils.DeviceID, deviceID);
        }
        return "and"+deviceID;
    }


    public static String getUniqueId(Context context){
        try {
            String androidID = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
            if(androidID == null || androidID.length() == 0){
                return "";
            }
            String serial = Build.SERIAL;
            if (serial == null || serial.length() == 0){
                return "";
            }
            String id = androidID + serial;
            return toMD5(id);
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }


    private static String toMD5(String text) throws NoSuchAlgorithmException {
        //获取摘要器 MessageDigest
        MessageDigest messageDigest = MessageDigest.getInstance("MD5");
        //通过摘要器对字符串的二进制字节数组进行hash计算
        byte[] digest = messageDigest.digest(text.getBytes());

        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < digest.length; i++) {
            //循环每个字符 将计算结果转化为正整数;
            int digestInt = digest[i] & 0xff;
            //将10进制转化为较短的16进制
            String hexString = Integer.toHexString(digestInt);
            //转化结果如果是个位数会省略0,因此判断并补0
            if (hexString.length() < 2) {
                sb.append(0);
            }
            //将循环结果添加到缓冲区
            sb.append(hexString);
        }
        //返回整个结果
        return sb.toString();
    }

    /**
     * 获取当前手机系统版本号
     *
     * @return  系统版本号
     */
    public static String getSystemVersion() {
        return android.os.Build.VERSION.RELEASE;
    }

    /**
     * 获取手机型号
     *
     * @return  手机型号
     */
    public static String getSystemModel() {
        return android.os.Build.MODEL;
    }

    /**
     * 获取手机厂商
     *
     * @return  手机厂商
     */
    public static String getDeviceBrand() {
        return android.os.Build.BRAND;
    }
}
